package com.parking.action;

import com.parking.bean.ParkAdmin;
import com.parking.service.ParkAdminService;
import com.parking.service.impl.ParkAdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/parkAdmin")
public class ParkAdminServlet extends HttpServlet {
    private ParkAdminService parkAdminService=new ParkAdminServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        switch (method){
            case "list":
                request.setAttribute("list",this.parkAdminService.list());
                request.getRequestDispatcher("parkadmin.jsp").forward(request,response);
            break;
            case "search":
                String key=request.getParameter("key");
                String value=request.getParameter("value");
                request.setAttribute("list",this.parkAdminService.search(key,value));
                request.getRequestDispatcher("parkadmin.jsp").forward(request,response);
                break;
            case "save":
                String username=request.getParameter("username");
                String password=request.getParameter("password");
                String name=request.getParameter("name");
                String gender=request.getParameter("gender");
                String telephone=request.getParameter("telephone");
                this.parkAdminService.save(new ParkAdmin(username,password,name,gender,telephone));
                response.sendRedirect("/parkAdmin?method=list");
                break;
            case "update":
                String id=request.getParameter("id");
                username=request.getParameter("username");
                password=request.getParameter("password");
                name=request.getParameter("name");
                gender=request.getParameter("gender");
                telephone=request.getParameter("telephone");
                this.parkAdminService.update(new ParkAdmin(id,username,password,name,gender,telephone));
                response.sendRedirect("/parkAdmin?method=list");
                break;
            case "delete":
                id=request.getParameter("id");
                this.parkAdminService.delete(id);
                response.sendRedirect("/parkAdmin?method=list");
                break;
        }

    }
}
